package com.ship.config;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
import com.ship.core.processor.DecryptEncryptFastJsonHttpMessageConverter;
import com.ship.core.processor.RequestDecryptResponseEncryptBodyProcessor;
import com.ship.core.processor.RequestResponseBodyMethodProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * mvc 配置
 */
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
    }


    @Bean
    public RequestDecryptResponseEncryptBodyProcessor requestDecryptResponseEncryptBodyProcessor() {
        return new RequestDecryptResponseEncryptBodyProcessor();
    }

    @Bean
    public DecryptEncryptFastJsonHttpMessageConverter decryptEncryptFastJsonHttpMessageConverter(){
        DecryptEncryptFastJsonHttpMessageConverter decryptEncryptFastJsonHttpMessageConverter = new DecryptEncryptFastJsonHttpMessageConverter(requestDecryptResponseEncryptBodyProcessor());
        return decryptEncryptFastJsonHttpMessageConverter;
    }

    @Bean
    public GsonHttpMessageConverter gsonHttpMessageConverter() {
        GsonHttpMessageConverter gsonHttpMessageConverter = new GsonHttpMessageConverter();
        return gsonHttpMessageConverter;
    }


    @Bean
    public RequestResponseBodyMethodProcessor requestDecryptResponseEncryptBodyMethodProcessor() {
        List<HttpMessageConverter<?>> list = new ArrayList<>();
        list.add(decryptEncryptFastJsonHttpMessageConverter());
        RequestResponseBodyMethodProcessor requestResponseBodyMethodProcessor = new RequestResponseBodyMethodProcessor(list);
        return requestResponseBodyMethodProcessor;
    }

    @Bean
    public FastJsonHttpMessageConverter4 fastJsonHttpMessageConverter4(){
        return  new FastJsonHttpMessageConverter4();
    }

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(fastJsonHttpMessageConverter4());
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(requestDecryptResponseEncryptBodyMethodProcessor());
    }

    @Override
    public void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
        returnValueHandlers.add(requestDecryptResponseEncryptBodyMethodProcessor());
    }


    @Bean(name = "messageSource")
    public ResourceBundleMessageSource getMessageResource() {
        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
        messageSource.setBasename("i18n/messages");
        return messageSource;
    }

}
